﻿using System.Collections.Generic;
using System.Linq;
using DesignPatterns.BuilderPattern.Models;

namespace DesignPatterns.BuilderPattern.Repositories
{
    public interface IPersonBuilder : IBuilder<Person>
    {
        //This should only be used by the person repository
    }

    public interface IPersonRepository 
    {
        List<Person> GetAll();
        Person GetById(int id);
    }

    public class PersonRepository : IPersonRepository
    {
        public List<IPersonBuilder> Builders { get; set; }

        public PersonRepository(IEnumerable<IPersonBuilder> builders)
        {
            Builders = builders.ToList();
        }

        public List<Person> GetAll()
        {
            //Not used in this demo;
            return new List<Person>();
        } 

        public Person GetById(int id)
        {
            //This is where we would get the person row from the database
            var person = new Person
            {
                FirstName = "Jack", LastName = "Bouer"
            };

            //Each builder only worries about what their responsible for
            //It shouldn't matter what order these are called in
            Builders.ForEach(i => i.Populate(ref person));
            return person;
        }
    }
}
